﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using CrystalDecisions.Web;
using Eassets.Service.Asset;
using Eassets.Layered.Domain.Asset;
using Eassets.UI.Asset.Reports;
using System.ComponentModel;
using Eassets.Service.Asset;


namespace Eassets.UI.Asset
{
    public partial class ReportViewerForm : System.Web.UI.Page
    {
        private int intZoom = 100;
        private String strCaption;
        private String strMainForm;
        DataTable dataTable = new DataTable("Header");
        DataTable dataTableObject = new DataTable("Object");
        DataRow dr;
        private static int count = 0;


        public String setReportSource
        {
            set
            {
                CrystalReportViewer1.ReportSource = value;
            }
        }


        public String MainFormID
        {
            set
            {
                CrystalReportViewer1.ReportSource = value;
            }
        }

        public String Caption
        {
            set
            {
                CrystalReportViewer1.ReportSource = value;
            }
        }

        public String setReportZoom
        {
            set
            {
                CrystalReportViewer1.ReportSource = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                Session.Remove("ObjRpt");
                if (Session["Print"] == "PrintTranSummary")
                {
                    //TransactionSummaryTable();

                    ViewState["TransactionDocumentCode"] = String.IsNullOrEmpty(Request.QueryString.Get("TransactionDocumentCode"))
                                            ? String.Empty
                                            : Request.QueryString.Get("TransactionDocumentCode");

                    ViewState["formName"] = String.IsNullOrEmpty(Request.QueryString.Get("mainform"))
                                           ? String.Empty
                                           : Request.QueryString.Get("mainform");

                    dr = dataTable.NewRow();

                    dataTable.Columns.Clear();
                    dataTableObject.Columns.Clear();
                    count = 0;
                    CreateColumn(ViewState["TransactionDocumentCode"].ToString(), "TransactionDocumentCode");

                    dataTable.Rows.Add(dr);

                    GetTransactionSummaryData(ViewState["TransactionDocumentCode"].ToString());
                }

                else
                {

                    ViewState["OrganizationLevelListId"] = String.IsNullOrEmpty(Request.QueryString.Get("OrganizationLevelListId"))
                                             ? String.Empty
                                             : Request.QueryString.Get("OrganizationLevelListId");

                    ViewState["AssetId"] = String.IsNullOrEmpty(Request.QueryString.Get("AssetId"))
                                                ? String.Empty
                                                : Request.QueryString.Get("AssetId");

                    ViewState["formName"] = String.IsNullOrEmpty(Request.QueryString.Get("mainform"))
                                                ? String.Empty
                                                : Request.QueryString.Get("mainform");
                    //ViewState["transactionNo"] = String.IsNullOrEmpty(Request.QueryString.Get("tranno"))
                    //                          ? String.Empty
                    //                          : Request.QueryString.Get("tranno");
                    ViewState["mode"] = String.IsNullOrEmpty(Request.QueryString.Get("mod"))
                                            ? String.Empty
                                            : Request.QueryString.Get("mod");
                    ViewState["transactionType"] = String.IsNullOrEmpty(Request.QueryString.Get("ttype"))
                                                       ? String.Empty
                                                       : Request.QueryString.Get("ttype");
                    ViewState["document"] = String.IsNullOrEmpty(Request.QueryString.Get("doc"))
                                                ? String.Empty
                                                : Request.QueryString.Get("doc");
                    ViewState["businessUnit"] = String.IsNullOrEmpty(Request.QueryString.Get("busUnit"))
                                                    ? String.Empty
                                                    : Request.QueryString.Get("busUnit");
                    ViewState["businessUnitName"] = String.IsNullOrEmpty(Request.QueryString.Get("busUName"))
                                                        ? String.Empty
                                                        : Request.QueryString.Get("busUName");
                    ViewState["businessUnitCode"] = String.IsNullOrEmpty(Request.QueryString.Get("busUCode"))
                                                        ? String.Empty
                                                        : Request.QueryString.Get("busUCode");

                    ViewState["fromDate"] = String.IsNullOrEmpty(Request.QueryString.Get("fromdt"))
                                                ? String.Empty
                                                : Request.QueryString.Get("fromdt");
                    ViewState["toDate"] = String.IsNullOrEmpty(Request.QueryString.Get("todt"))
                                              ? String.Empty
                                              : Request.QueryString.Get("todt");
                    ViewState["reqmode"] = String.IsNullOrEmpty(Request.QueryString.Get("reqmode"))
                                               ? String.Empty
                                               : Request.QueryString.Get("reqmode");

                    dr = dataTable.NewRow();

                    dataTable.Columns.Clear();
                    dataTableObject.Columns.Clear();
                    count = 0;
                    CreateColumn(ViewState["OrganizationLevelListId"].ToString(), "OrganizationLevelListId");
                    CreateColumn(ViewState["AssetId"].ToString(), "AssetId");
                    CreateColumn(ViewState["formName"].ToString(), "formName");
                    //CreateColumn(ViewState["transactionNo"].ToString(), "transactionNo");
                    CreateColumn(ViewState["mode"].ToString(), "mode");
                    CreateColumn(ViewState["transactionType"].ToString(), "transactionType");
                    CreateColumn(ViewState["document"].ToString(), "document");
                    CreateColumn(ViewState["businessUnit"].ToString(), "businessUnit");
                    CreateColumn(ViewState["businessUnitName"].ToString(), "businessUnitName");
                    CreateColumn(ViewState["businessUnitCode"].ToString(), "businessUnitCode");
                    CreateColumn(ViewState["fromDate"].ToString(), "fromDate");
                    CreateColumn(ViewState["toDate"].ToString(), "toDate");
                    CreateColumn(ViewState["reqmode"].ToString(), "reqmode");
                    dataTable.Rows.Add(dr);


                    GetMainData(ViewState["formName"].ToString(), Convert.ToInt64(ViewState["OrganizationLevelListId"]), Convert.ToInt64(ViewState["AssetId"]));
                }


            }
            if (ViewState["formName"].ToString() == "FixedAssetRegistry")
            {
                GetMainData(ViewState["formName"].ToString(), 0, 0);
            }
            if (ViewState["formName"].ToString() == "TransactionSummary")
            {
                GetTransactionSummaryData(ViewState["formName"].ToString());
            }
            
        }


        protected void CreateColumn(String Column, String ColumnName)
        {
            String value = "";
            if (!String.IsNullOrEmpty(Column))
            {
                value = Column;
            }
            dataTable.Columns.Add(new DataColumn(ColumnName, typeof(String)));

            dr[count] = Column;
            count++;

        }

        protected void GetMainData(String pageName, long? OrganizationLevelListId, long? AssetId)
        {
            DataSet dataSet = new DataSet();
            //if (pageName == "salesforceform")
            //{
            this.Title = "Fixed Asset Registry";
            if (Session["ObjRpt"] == null && ViewState["formName"].ToString() == "FixedAssetRegistry")
            {

                Reports.FixedAssetRegistry objRpt;
                AssetItemDetailsService assetItemDetailsService = new AssetItemDetailsService();
                Collection<AssetItemDetails> assetRegistry = assetItemDetailsService.GetAllAssetItemDetailsByOrganizationLevelListId(Convert.ToInt64(OrganizationLevelListId), Convert.ToInt64(AssetId));

                //dataTableObject = ConvertTo(assetRegistry);
                DataTable dataTable = new DataTable();

                dataTable.Columns.Add("Code", typeof(string));
                dataTable.Columns.Add("AssetSerialCode", typeof(string));
                dataTable.Columns.Add("AssetStatus", typeof(string));
                //dataTable.Columns.Add("Description", typeof(string));
                dataTable.Columns.Add("PurchasedDate", typeof(DateTime));

                dataTable.Columns.Add("PurchasedValue", typeof(decimal));

                dataTable.Columns.Add("DepreciatedValue", typeof(decimal));
                dataTable.Columns.Add("CumulativeDepValue", typeof(decimal));
                dataTable.Columns.Add("CurrentBookValue", typeof(decimal));

                dataTable.Columns.Add("RemainingLifeTime", typeof(int));
                //dataTable.Columns.Add("RemainingLifetimeUOM", typeof(string));

                foreach (AssetItemDetails assetItemDetails in assetRegistry)
                {
                    Assets assets = new Assets();

                    // DataRow dr=new DataRow();
                    dataTable.Rows.Add(assetItemDetails.Assets.Code, assetItemDetails.AssetSerialCode, assetItemDetails.AssetStatus, assetItemDetails.PurchasedDate, assetItemDetails.PurchasedValue,
                        assetItemDetails.DepreciatedValue, assetItemDetails.CumulativeDepValue, assetItemDetails.CurrentBookValue, assetItemDetails.RemainingLifeTime);


                }


                dataSet.Tables.Add(dataTable.Copy());
                dataSet.Tables.Add(dataTableObject.Copy());
                objRpt = new FixedAssetRegistry();
                //dataSet.WriteXmlSchema("E:/SANJAYA/CrystalReport/FixedAssetRegistry.xsd");
                SendPrintData(dataSet, objRpt);
            }
            else
            {
                CrystalReportViewer1.ReportSource = (FixedAssetRegistry)Session["ObjRpt"];
            }
            //}


        }

        protected void SendPrintData(DataSet dataSetSend, ReportClass report)
        {

            report.SetDataSource(dataSetSend);
            Session["ObjRpt"] = report;
            CrystalReportViewer1.ReportSource = report;
            // DirectCast(objRpt, SalesForcesReport).PrintOptions.PaperSize = PaperSize.PaperB5;

        }

        public static DataTable ConvertTo<T>(IList<T> list)
        {
            DataTable table = CreateTable<T>();
            Type entityType = typeof(T);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (T item in list)
            {
                DataRow row = table.NewRow();

                foreach (PropertyDescriptor prop in properties)
                {
                    row[prop.Name] = prop.GetValue(item);
                }

                table.Rows.Add(row);
            }

            return table;
        }

        public static DataTable CreateTable<T>()
        {
            Type entityType = typeof(T);
            DataTable table = new DataTable(entityType.Name);
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);

            foreach (PropertyDescriptor prop in properties)
            {
                // HERE IS WHERE THE ERROR IS THROWN FOR NULLABLE TYPES
                table.Columns.Add(prop.Name, prop.PropertyType);
            }

            return table;
        }

        //private void TransactionSummaryTable()
        //{
        //    //ViewState["Transaction"] = String.IsNullOrEmpty(Request.QueryString.Get("Transaction"))
        //    //                                 ? String.Empty
        //    //                                 : Request.QueryString.Get("Transaction");
        //    ViewState["TransactionId"] = String.IsNullOrEmpty(Request.QueryString.Get("TransactionId"))
        //                                     ? String.Empty
        //                                     : Request.QueryString.Get("TransactionId");

        //    ViewState["TransactionNo"] = String.IsNullOrEmpty(Request.QueryString.Get("TransactionNo"))
        //                                 ? String.Empty
        //                                 : Request.QueryString.Get("mainform");

        //    ViewState["TransactionDate"] = String.IsNullOrEmpty(Request.QueryString.Get("TransactionDate"))
        //                               ? String.Empty
        //                               : Request.QueryString.Get("TransactionDate");



        //    dr = dataTable.NewRow();

        //    dataTable.Columns.Clear();
        //    dataTableObject.Columns.Clear();
        //    count = 0;
        //    //CreateColumn(ViewState["Transaction"].ToString(), "Transaction");
        //    CreateColumn(ViewState["TransactionId"].ToString(), "TransactionId");
        //    CreateColumn(ViewState["TransactionNo"].ToString(), "TransactionNo");
        //    CreateColumn(ViewState["TransactionDate"].ToString(), "TransactionDate");
        //    CreateColumn(ViewState["EmployeeName"].ToString(), "EmployeeName");

        //    dataTable.Rows.Add(dr);
        //}

        protected void GetTransactionSummaryData(string documentCode)
        {
            DataSet dataSet = new DataSet();

            this.Title = "Transaction Summary";
            if (Session["ObjRpt"] == null)
            {

                Reports.TransactionSummary objRpt;
                TransactionService transactionService = new TransactionService();
                List<Transaction> transactionRegistry =
                    transactionService.GetAllTransactionByTransactionDocumentCode(documentCode).ToList();

                DataTable dataTable = new DataTable();

                dataTable.Columns.Add("TransactionId", typeof(long));
                dataTable.Columns.Add("TransactionNo", typeof(string));
                dataTable.Columns.Add("TransactionDate", typeof(DateTime));
                dataTable.Columns.Add("EmployeeName", typeof(string));

               
                
                foreach (Transaction transaction in transactionRegistry)
                {
                    string employeeName;
                    employeeName = transactionService.GetEmployeeNameByTransactionId(transaction.TransactionId);
                    dataTable.Rows.Add(transaction.TransactionId, transaction.TransactionNo, transaction.TransactionDate, employeeName);

                }

                dataSet.Tables.Add(dataTable.Copy());
                dataSet.Tables.Add(dataTableObject.Copy());
                objRpt = new TransactionSummary();
                dataSet.WriteXmlSchema("D:/Cry/TransactionSummery/TransactionSummary.xsd");
                SendPrintData(dataSet, objRpt);

            }
            else
            {
                CrystalReportViewer1.ReportSource = (TransactionSummary)Session["ObjRpt"];
            }
            //}


        }
    }
}